// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_MST_WORKING_GROUP
// * CreateDate    : 2011/9/27 13:27:06
// * Creater       : CodeSmith
// * LastChangeDate: 2011/9/27 13:27:06
// * LastChanger   : 2011/9/27 13:27:06
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;
using ESOL.Common;

namespace ESOL.Business.Mst
{
    /// <summary>
    /// MstWorkingGroup
    /// </summary>
    public partial class MstWorkingGroupBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objMstWorkingGroupEntity">Entity</param>
        /// <returns>Entity</returns>
        public static MstWorkingGroupEntity GetMstWorkingGroup(MstWorkingGroupEntity objMstWorkingGroupEntity)
        {
            return DataAccess.SelectSingle<MstWorkingGroupEntity>(objMstWorkingGroupEntity);
        }
        #endregion


        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objMstWorkingGroupEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertMstWorkingGroup(MstWorkingGroupEntity objMstWorkingGroupEntity)
        {
            return DataAccess.Insert(objMstWorkingGroupEntity);
        }

        public static bool InsertMstWorkingGroup(List<MstWorkingGroupEntity> objMstWorkingGroupEntityList)
        {
            return DataAccess.Insert(objMstWorkingGroupEntityList);
        }
        #endregion


        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objMstWorkingGroupEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateMstWorkingGroup(MstWorkingGroupEntity objMstWorkingGroupEntity)
        {
            return DataAccess.Update(objMstWorkingGroupEntity);
        }

        public static bool UpdateMstWorkingGroup(List<MstWorkingGroupEntity> objMstWorkingGroupEntityList)
        {
            return DataAccess.Update<MstWorkingGroupEntity>(objMstWorkingGroupEntityList);
        }
        #endregion


        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objMstWorkingGroupEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteMstWorkingGroup(MstWorkingGroupEntity objMstWorkingGroupEntity)
        {
            return DataAccess.Delete(objMstWorkingGroupEntity);
        }

        public static bool DeleteMstWorkingGroup(List<MstWorkingGroupEntity> objMstWorkingGroupEntityList)
        {
            return DataAccess.Delete<MstWorkingGroupEntity>(objMstWorkingGroupEntityList);
        }
        #endregion


        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetMstWorkingGroupAll()
        {
            return DataAccess.SelectAllDataSet<MstWorkingGroupEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<MstWorkingGroupEntity> GetMstWorkingGroupAllList()
        {
            return DataAccess.SelectAll<MstWorkingGroupEntity>();
        }
        #endregion

        #region Funtions
        public static MstWorkingGroupEntity GetMstWorkingGroupByID(string ID)
        {
            if (string.IsNullOrEmpty(ID))
                return null;
            MstWorkingGroupEntity m = new MstWorkingGroupEntity();
            m.GroupId = ID;
            return DataAccess.SelectSingle<MstWorkingGroupEntity>(m);
        }

        public static DataTable GetDtMstWorkingGroupSummaryByCondition(string Condition, string rowCount, string OrderBy)
        {
            string Sql = CommonFunctions.GetSelect(rowCount) + @" MWG.*,MP.NAME_CN,MP.PERSON_CODE FROM MST_WORKING_GROUP MWG 
                LEFT JOIN MST_PERSON MP ON MWG.GROUP_LEADER=MP.PERSON_ID WHERE 1=1 ";
            if (!string.IsNullOrEmpty(Condition))
                Sql += Condition;
            if (!string.IsNullOrEmpty(OrderBy))
                Sql += OrderBy;
            DataSet ds = DataAccess.SelectDataSet(Sql);
            if (ds != null && ds.Tables[0] != null)
                return ds.Tables[0];
            else return null;
        }

        public static DataTable GetDtMstWorkingGroupByCondition(string Condition, string OrderBy)
        {
            string Sql = @"SELECT MWG.*,MP2.NAME_CN AS LEADER_NAME_CN,
                MWGP.PERSON_ID,MP.NAME_CN,MP.PERSON_CODE,MP.GENDER,MD.DEPARTMENT_NAME 
                FROM MST_WORKING_GROUP MWG
                LEFT JOIN MST_WORKING_GROUP_PERSON MWGP ON MWG.GROUP_ID=MWGP.GROUP_ID 
                LEFT JOIN MST_PERSON MP ON MWGP.PERSON_ID=MP.PERSON_ID 
                LEFT JOIN MST_PERSON MP2 ON MWG.GROUP_LEADER=MP2.PERSON_ID 
                LEFT JOIN MST_DEPARTMENT MD ON MP.DEPARTMENT_ID=MD.DEPARTMENT_ID
                WHERE 1=1 ";
            if (!string.IsNullOrEmpty(Condition))
                Sql += Condition;
            if (!string.IsNullOrEmpty(OrderBy))
                Sql += OrderBy;
            DataSet ds = DataAccess.SelectDataSet(Sql);
            if (ds != null && ds.Tables[0] != null)
                return ds.Tables[0];
            else return null;
        }

        public static bool SaveWorkingGroups(bool isNew,MstWorkingGroupEntity entity,List<MstWorkingGroupPersonEntity> list)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    if (isNew)
                    {
                        DataAccess.Insert(entity, broker);
                        DataAccess.Insert(list, broker);
                    }
                    else
                    {
                        DataAccess.Update(entity, broker);
                        string Sql=@"select * from MST_WORKING_GROUP_PERSON mwgp
                            where mwgp.GROUP_ID='"+entity.GroupId+"'";
                        List<MstWorkingGroupPersonEntity> listWorkingGroupPerson = DataAccess.Select<MstWorkingGroupPersonEntity>(Sql);
                        DataAccess.Delete<MstWorkingGroupPersonEntity>(listWorkingGroupPerson, broker);
                        DataAccess.Insert<MstWorkingGroupPersonEntity>(list, broker);
                    }
                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
        }

        public static bool DelWorkingGroups(MstWorkingGroupEntity entity)
        {
            if (entity == null)
                return false;
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();                    
                    List<MstWorkingGroupPersonEntity> list = new List<MstWorkingGroupPersonEntity>();
                    list = MstWorkingGroupPersonBs.GetListMstWorkingGroupPersonByGroupID(entity.GroupId);
                    DataAccess.Delete<MstWorkingGroupPersonEntity>(list,broker);
                    DataAccess.Delete(entity, broker);
                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
        }

        #endregion
    }


}

